//
//  VehicleDetailViewController.m
//  Runone
//
//  Created by runone02 on 16/8/19.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "VehicleDetailViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>

#import "Define.h"
#import "RequestHelper.h"
#import "BDVehicleDetailInfo.h"

#import "CustomAnnotationView.h"
#import "ROPointAnnotation.h"
#import "AppDelegate.h"


@interface VehicleDetailViewController ()<UITableViewDelegate, UITableViewDataSource,MAMapViewDelegate>

@property (nonatomic, strong) MAMapView *mapView; //地图

@property (nonatomic, strong) UITableView *vehicleInfoTableView;//

@property (strong, nonatomic)NSMutableArray *dataArray;


@end
@implementation VehicleDetailViewController {
    BDVehicleDetailInfo *vehicleDetailInfo;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configView];
    self.title=@"车辆信息详情";
    [self loadMapView]; //加载地图
    [self defaultAnnimation]; //设置到广乐路段
    [self loadAnnotation]; //显示车辆图标
    [self requestData];  // 请求
//    [self showCameraGunArround2KM];//在地图上显示2000m内所有的摄像枪
    
    self.automaticallyAdjustsScrollViewInsets  = NO;
}

- (void)configView {
    self.vehicleInfoTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 260, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    self.vehicleInfoTableView.dataSource = self;
    self.vehicleInfoTableView.delegate   = self;
    self.vehicleInfoTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    [self.view addSubview:self.vehicleInfoTableView];
    
}

- (void)loadMapView {
    [AMapServices sharedServices].apiKey = GETGAODEPAIKEY;
    
    _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,250)];
    _mapView.delegate = self;
    _mapView.showsCompass = NO;
    _mapView.showsScale= NO;
    [_mapView setZoomLevel:10];
    [self.view addSubview:_mapView];
}
-(void)defaultAnnimation
{
    MACoordinateSpan span = MACoordinateSpanMake(0.08, 0.08);
    CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(25.1902561, 113.212982);
    MACoordinateRegion region = MACoordinateRegionMake(centerCoordinate, span);
    _mapView.region = region;
}
- (void)loadAnnotation {
    ROPointAnnotation *pointAnnotation = [[ROPointAnnotation alloc] init];
    NSMutableArray *pointAnnotationArray = [[NSMutableArray alloc]init];
    float latitude = [self.model.Latitude floatValue];
    float longitude = [self.model.Longitude floatValue];
    pointAnnotation.coordinate=CLLocationCoordinate2DMake(latitude,longitude);
    [pointAnnotationArray addObject:pointAnnotation];
    [_mapView addAnnotation:pointAnnotation];
    [_mapView showAnnotations:pointAnnotationArray animated:YES];
}

- (void)requestData {
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *GetAllLastestVehicleDynamicModel=[[RequestGinsengModel alloc] init];
    GetAllLastestVehicleDynamicModel.URL=GetAllLastestVehicleDynamicURL;
    GetAllLastestVehicleDynamicModel.DataValue= [self DataValueStr:self.model.VehicleNo];
    GetAllLastestVehicleDynamicModel.UpdatedTime=@"2012-01-12 12:00:00";
    GetAllLastestVehicleDynamicModel.SystemCode=@"GuangleHighway";
    GetAllLastestVehicleDynamicModel.MethodName=@"GetBDVehicleDetailInfo";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        vehicleDetailInfo = dataValueModel;
        [self.vehicleInfoTableView reloadData];
    };
    [requestHelper RequestGinsengModel:GetAllLastestVehicleDynamicModel];
}
//- (void)drawCircle {
//    CLLocationDistance radius = 2000;
//    CGFloat latitude = [self.model.Latitude floatValue];
//    CGFloat longtitude = [self.model.Longitude floatValue];
//    CLLocationCoordinate2D coordinate2D = CLLocationCoordinate2DMake(latitude, longtitude);
//    MACircle *circle = [MACircle circleWithCenterCoordinate:coordinate2D radius:radius];
//    [_mapView addOverlay:circle];
//}

#pragma mark -判断摄像枪是否在车辆为中心的2000m的半径内
- (void)showCameraGunArround2KM {
    
    AppDelegate *myDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate ;
    NSArray *dataSourceArray=myDelegate.AllDeviceBaseArray ;
    NSMutableArray *cameraPointsArray = [[NSMutableArray alloc] init];//存放在区域内的摄像枪点
    CGFloat latitude = [self.model.Latitude floatValue];
    CGFloat longtitude = [self.model.Longitude floatValue];
    CLLocationCoordinate2D coordinate2D = CLLocationCoordinate2DMake(latitude, longtitude);
    MAMapPoint vehiclePoint = MAMapPointForCoordinate(coordinate2D);
    
    for (DeviceBaseInfo *baseInfoModel in dataSourceArray) {
        CLLocationCoordinate2D coordinate =CLLocationCoordinate2DMake(baseInfoModel.Latitude, baseInfoModel.Longitude);
        MAMapPoint point1 = MAMapPointForCoordinate(coordinate);
        CLLocationDistance distance = MAMetersBetweenMapPoints(point1,vehiclePoint);
        
        if (distance <= 2000) {
            ROPointAnnotation *pointAnnotation = [[ROPointAnnotation alloc] init];
            float latitude = [self.model.Latitude floatValue];
            float longitude = [self.model.Longitude floatValue];
            pointAnnotation.coordinate=CLLocationCoordinate2DMake(latitude,longitude);
            NSLog(@"%f----%f",pointAnnotation.coordinate.latitude,pointAnnotation.coordinate.longitude);
            [cameraPointsArray addObject:pointAnnotation];//添加到数组中
        }
    }
    [_mapView addAnnotations:cameraPointsArray];
    [_mapView showAnnotations:cameraPointsArray animated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"cell";
    UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = [NSString stringWithFormat:@"车辆号码：%@",vehicleDetailInfo.VehicleNo];
            break;
        case 1:
            cell.textLabel.text  = [NSString stringWithFormat:@"车辆类型：%ld",(long)vehicleDetailInfo.VehicleType];
            break;
        case 2:
            cell.textLabel.text  = [NSString stringWithFormat:@"货物类型：%ld",(long)vehicleDetailInfo.GoodsType];
            break;
        case 3:
            cell.textLabel.text  = [NSString stringWithFormat:@"所属公司：%@",vehicleDetailInfo.Company];
            break;
        case 4:
            cell.textLabel.text  = [NSString stringWithFormat:@"联系人：%@",vehicleDetailInfo.Contactor];
            break;
        case 5:
            cell.textLabel.text  = [NSString stringWithFormat:@"联系电话：%@",vehicleDetailInfo.Contact];
            break;
        default:
            cell.textLabel.text  = [NSString stringWithFormat:@"通讯地址：%@",vehicleDetailInfo.Address];
            break;
    }
    return cell;
}
#pragma mark -地图代理方法
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        CustomAnnotationView *annotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil)
        {
            annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        annotationView.canShowCallout= NO;       //设置气泡可以弹出，默认为NO
        annotationView.image = [UIImage imageNamed:@"restaurant"];
        return annotationView;
    }
    return nil;
}

#pragma  mark - overlay 添加overlay
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id<MAOverlay>)overlay {
    if ([overlay isKindOfClass:[MACircle class]]){
        MACircleRenderer *circleRenderer = [[MACircleRenderer alloc]initWithCircle:overlay];
        circleRenderer.fillColor = [UIColor colorWithRed:1.0 green:0.8 blue:0.0 alpha:0.8];
        circleRenderer.strokeColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.8];;
        circleRenderer.lineWidth = 5.f;
        circleRenderer.lineDash = YES;
        
        return circleRenderer;
    }
    return nil;
}

-(NSString *)DataValueStr:(NSString *)str
{
    NSString *beginstr=[NSString stringWithFormat:@"[{\"key\":\"VehicleNo\",\"value\":\"%@\"}]",str];
    
    return beginstr;
}
@end
